<?php

namespace addons\zzyy\controller\api;


use addons\zzyy\library\Ykxbon;
use app\admin\model\zzyy\ZzyyReserve;
use app\admin\model\zzyy\ZzyyRoomclose;
use app\admin\model\zzyy\ZzyyRoom;
use fast\Date;

class Room extends \app\common\controller\Api
{
    protected $noNeedLogin = ['*'];

    public function baseinfo()
    {
        $id = $this->request->get('id', null);

        $room = \addons\zzyy\module\Room::getRoomFormatInfo($id);

        $this->success('OK',  $room);
    }

    public function getRentInfo()
    {
        $roomId = $this->request->get('roomid');
        $date = $this->request->get('date', Ykxbon::now()->toDateString());

        $dateParse = Ykxbon::parse($date);

        $dateStart = $dateParse->startOfDay()->toDateTimeString();
        $dateEnd = $dateParse->endOfDay()->toDateTimeString();


        //得到当天已经预约的时间 包含了已经 下定单，还没有付款的
        $reservedTimes = ZzyyReserve::where('room_id', $roomId)
            ->whereIn('status', [ZzyyReserve::STATUS_CREATED, ZzyyReserve::STATUS_SUCCESS]) //正常的
            ->where('starttime', 'between', [$dateStart, $dateEnd])
            ->select();

        //得到当天不可预约的时间
        $closeTimes = ZzyyRoomclose::where('roomclose_starttime', 'between', [$dateStart, $dateEnd])
            ->whereOr('roomclose_endtime', 'between', [$dateStart, $dateEnd])
            ->select();

        $room = ZzyyRoom::get($roomId);
        $storeId = $room->store_id;

        $closeTimes = array_filter($closeTimes, function ($e) use ($storeId, $roomId) {
            if (0 == $e->store_id && 0 == $e->room_id) {
                return true;
            } else if (0 == $e->room_id) {
                return $storeId == $e->store_id ? true : false;
            } else if ($roomId == $e->room_id) {
                return true;
            }
            return false;
        });

        //得到当天的rent time
        $rentPills = empty($room->rent_time) ? [] : json_decode($room->rent_time, true);

        foreach ($rentPills as $k => $item) {
            // 得到这个pill的完整的开始和结束时间，包括的年月日
            list($hour, $min) = explode(':', $item['start']);
            $pillstart = Ykxbon::create($dateParse->getYear(), $dateParse->getMonth(), $dateParse->getDay(), $hour, $min);
            list($hour, $min) = explode(':', $item['end']);
            $pillend = Ykxbon::create($dateParse->getYear(), $dateParse->getMonth(), $dateParse->getDay(), $hour, $min);

            $rentPills[$k]['id'] = $k;

            $rentPills[$k]['state'] = 0;

            // 如果这个pill开始和结束时间之间有$closetimes的开始和结束时间 或之间，那么这个pill的state也设置为2
            foreach ($closeTimes as $closeTime) {
                $closeStartTime = Ykxbon::parse($closeTime->roomclose_starttime);
                $closeEndTime = Ykxbon::parse($closeTime->roomclose_endtime);
                if ($closeStartTime->betweenIncluded($pillstart, $pillend)) {
                    $rentPills[$k]['state'] = 2; // 2 表示已经占用了
                }
                if ($closeEndTime->betweenIncluded($pillstart, $pillend)) {
                    $rentPills[$k]['state'] = 2;
                }
                if ($closeStartTime->isBefore($pillstart)
                    and $closeEndTime->isAfter($pillend)) {
                    $rentPills[$k]['state'] = 2;
                }
            }

            // 如果这个pill开始和结果时间之间有$reserveTimes的开始和结束时间，那么这个pill的state设置为2
            foreach ($reservedTimes as $reservedTime) {
                $reserveStartTime = Ykxbon::parse($reservedTime->starttime);
                $reserveEndTime = Ykxbon::parse($reservedTime->endtime);

                if ($reserveStartTime->betweenIncluded($pillstart, $pillend)) {
                    $rentPills[$k]['state'] = 2; // 2 表示已经占用了
                } else if ($reserveEndTime->betweenIncluded($pillstart, $pillend)) {
                    $rentPills[$k]['state'] = 2;
                } else if ($reserveStartTime->isBefore($pillstart)
                    and $reserveEndTime->isAfter($pillend)) {
                    $rentPills[$k]['state'] = 2;
                }
            }
        }

        $rentPills = array_values($rentPills); // 防止key 不是从0开始，会在js的客户端当成对象解释

        $this->success('ok', $rentPills);

    }
}
